/**
 *
 * @copyright &copy; 2010 - 2025, Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V.
 * All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its
 *    contributors may be used to endorse or promote products derived from
 *    this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * We kindly request you to use one or more of the following phrases to refer to
 * foxBMS in your hardware, software, documentation or advertising materials:
 *
 * - "This product uses parts of foxBMS&reg;"
 * - "This product includes parts of foxBMS&reg;"
 * - "This product is derived from foxBMS&reg;"
 *
 */

/**
 * @file    test_bal.c
 * @author  foxBMS Team
 * @date    2020-08-05 (date of creation)
 * @updated 2025-03-31 (date of last update)
 * @version v1.9.0
 * @ingroup UNIT_TEST_IMPLEMENTATION
 * @prefix  TEST
 *
 * @brief   Test of the API of the balancing module
 * @details TODO
 *
 */

/*========== Includes =======================================================*/
#include "unity.h"
#include "Mockdatabase.h"
#include "Mockos.h"

#include "database_cfg.h"

#include "bal.h"

#include <stdbool.h>
#include <stdio.h>

/*========== Unit Testing Framework Directives ==============================*/
TEST_INCLUDE_PATH("../../src/app/application/bal")

/*========== Definitions and Implementations for Unit Test ==================*/
BAL_STATE_s initState;

/*========== Setup and Teardown =============================================*/
void setUp(void) {
    initState.timer                  = 0;
    initState.stateRequest           = BAL_STATE_NO_REQUEST;
    initState.lastState              = BAL_STATEMACH_UNINITIALIZED;
    initState.state                  = BAL_STATEMACH_UNINITIALIZED;
    initState.lastSubstate           = BAL_ENTRY;
    initState.substate               = BAL_ENTRY;
    initState.triggerEntry           = 0;
    initState.errorRequestCounter    = 0;
    initState.initializationFinished = STD_NOT_OK;
    initState.active                 = false;
    initState.balancingThreshold     = 0;
    initState.balancingAllowed       = false;
    initState.balancingGlobalAllowed = false;
}

void tearDown(void) {
}

/*========== Test Cases =====================================================*/
void testNoStateChanged(void) {
    BAL_SaveLastStates(&initState);
    TEST_ASSERT_EQUAL(initState.lastState, BAL_STATEMACH_UNINITIALIZED);
    TEST_ASSERT_EQUAL(initState.state, BAL_STATEMACH_UNINITIALIZED);
    TEST_ASSERT_EQUAL(initState.lastSubstate, BAL_ENTRY);
    TEST_ASSERT_EQUAL(initState.substate, BAL_ENTRY);
}

void testStateChanged(void) {
    initState.lastState = BAL_STATEMACH_UNINITIALIZED;
    initState.state     = BAL_STATEMACH_INITIALIZATION;
    BAL_SaveLastStates(&initState);
    TEST_ASSERT_EQUAL(initState.lastState, BAL_STATEMACH_INITIALIZATION);
    TEST_ASSERT_EQUAL(initState.state, BAL_STATEMACH_INITIALIZATION);
    TEST_ASSERT_EQUAL(initState.lastSubstate, BAL_ENTRY);
    TEST_ASSERT_EQUAL(initState.substate, BAL_ENTRY);
}

void testSubstateChanged(void) {
    initState.lastSubstate = BAL_ENTRY;
    initState.substate     = BAL_CHECK_IMBALANCES;
    BAL_SaveLastStates(&initState);
    TEST_ASSERT_EQUAL(initState.lastState, BAL_STATEMACH_UNINITIALIZED);
    TEST_ASSERT_EQUAL(initState.state, BAL_STATEMACH_UNINITIALIZED);
    TEST_ASSERT_EQUAL(initState.lastSubstate, BAL_CHECK_IMBALANCES);
    TEST_ASSERT_EQUAL(initState.substate, BAL_CHECK_IMBALANCES);
}

void testFiniteStateMachineCheckReentranceFunction(void) {
    OS_EnterTaskCritical_Expect();
    OS_ExitTaskCritical_Expect();
    uint8_t returnValue = 0;
    returnValue         = BAL_CheckReEntrance(&initState);
    TEST_ASSERT_EQUAL(returnValue, 0);
    OS_EnterTaskCritical_Expect();
    OS_ExitTaskCritical_Expect();
    returnValue = BAL_CheckReEntrance(&initState);
    TEST_ASSERT_EQUAL(returnValue, 0xFF);
}

void testFiniteStateMachineTransferStateRequestFunction(void) {
    OS_EnterTaskCritical_Expect();
    OS_ExitTaskCritical_Expect();
    BAL_STATE_REQUEST_e returnValue = BAL_STATE_ERROR_REQUEST;
    initState.stateRequest          = BAL_STATE_ERROR_REQUEST;
    returnValue                     = BAL_TransferStateRequest(&initState);
    TEST_ASSERT_EQUAL(returnValue, BAL_STATE_ERROR_REQUEST);
    TEST_ASSERT_EQUAL(initState.stateRequest, BAL_STATE_NO_REQUEST);
}

void testFiniteStateMachineCheckStateRequestFunction0(void) {
    BAL_RETURN_TYPE_e returnValue = BAL_OK;

    returnValue = BAL_CheckStateRequest(&initState, BAL_STATE_ERROR_REQUEST);
    TEST_ASSERT_EQUAL(returnValue, BAL_OK);
}
void testFiniteStateMachineCheckStateRequestFunction1(void) {
    BAL_RETURN_TYPE_e returnValue    = BAL_OK;
    initState.balancingGlobalAllowed = false;
    returnValue                      = BAL_CheckStateRequest(&initState, BAL_STATE_GLOBAL_ENABLE_REQUEST);
    TEST_ASSERT_EQUAL(initState.balancingGlobalAllowed, true);
    TEST_ASSERT_EQUAL(returnValue, BAL_OK);
}
void testFiniteStateMachineCheckStateRequestFunction2(void) {
    BAL_RETURN_TYPE_e returnValue    = BAL_OK;
    initState.balancingGlobalAllowed = true;
    returnValue                      = BAL_CheckStateRequest(&initState, BAL_STATE_GLOBAL_DISABLE_REQUEST);
    TEST_ASSERT_EQUAL(initState.balancingGlobalAllowed, false);
    TEST_ASSERT_EQUAL(returnValue, BAL_OK);
}
void testFiniteStateMachineCheckStateRequestFunction3(void) {
    BAL_RETURN_TYPE_e returnValue = BAL_OK;
    returnValue                   = BAL_CheckStateRequest(&initState, BAL_STATE_NO_BALANCING_REQUEST);
    TEST_ASSERT_EQUAL(returnValue, BAL_OK);
}
void testFiniteStateMachineCheckStateRequestFunction4(void) {
    BAL_RETURN_TYPE_e returnValue = BAL_OK;
    returnValue                   = BAL_CheckStateRequest(&initState, BAL_STATE_ALLOW_BALANCING_REQUEST);
    TEST_ASSERT_EQUAL(returnValue, BAL_OK);
}
void testFiniteStateMachineCheckStateRequestFunction5(void) {
    BAL_RETURN_TYPE_e returnValue = BAL_OK;
    initState.state               = BAL_STATEMACH_UNINITIALIZED;
    returnValue                   = BAL_CheckStateRequest(&initState, BAL_STATE_INIT_REQUEST);
    TEST_ASSERT_EQUAL(returnValue, BAL_OK);
}

void testFiniteStateMachineCheckStateRequestFunction6(void) {
    BAL_RETURN_TYPE_e returnValue = BAL_OK;
    initState.state               = BAL_STATEMACH_INITIALIZED;
    returnValue                   = BAL_CheckStateRequest(&initState, BAL_STATE_INIT_REQUEST);
    TEST_ASSERT_EQUAL(returnValue, BAL_ALREADY_INITIALIZED);
}

void testFiniteStateMachineCheckStateRequestFunction7(void) {
    /* illegal request */
    BAL_RETURN_TYPE_e returnValue = BAL_OK;
    initState.stateRequest        = BAL_STATE_NO_REQUEST;
    returnValue                   = BAL_CheckStateRequest(&initState, BAL_STATE_NO_REQUEST);
    TEST_ASSERT_EQUAL(returnValue, BAL_ILLEGAL_REQUEST);
}

void testProcessStateUninitialized0(void) {
    BAL_ProcessStateUninitialized(&initState, BAL_STATE_INIT_REQUEST);
    TEST_ASSERT_EQUAL(initState.timer, BAL_STATEMACH_SHORTTIME_100ms);
    TEST_ASSERT_EQUAL(initState.state, BAL_STATEMACH_INITIALIZATION);
    TEST_ASSERT_EQUAL(initState.substate, BAL_ENTRY);
}

void testProcessStateUninitialized1(void) {
    BAL_ProcessStateUninitialized(&initState, BAL_STATE_NO_REQUEST);
}

void testProcessStateUninitialized2(void) {
    initState.errorRequestCounter = 0xFE;
    BAL_ProcessStateUninitialized(&initState, BAL_STATE_ERROR_REQUEST);
    TEST_ASSERT_EQUAL(initState.errorRequestCounter, 0xFF);
}

void testProcessStateInitialization(void) {
    BAL_ProcessStateInitialization(&initState);
    TEST_ASSERT_EQUAL(initState.timer, BAL_STATEMACH_SHORTTIME_100ms);
    TEST_ASSERT_EQUAL(initState.state, BAL_STATEMACH_INITIALIZED);
    TEST_ASSERT_EQUAL(initState.substate, BAL_ENTRY);
}

void testProcessStateInitialized(void) {
    BAL_ProcessStateInitialized(&initState);
    TEST_ASSERT_EQUAL(initState.initializationFinished, STD_OK);
    TEST_ASSERT_EQUAL(initState.timer, BAL_STATEMACH_SHORTTIME_100ms);
    TEST_ASSERT_EQUAL(initState.state, BAL_STATEMACH_CHECK_BALANCING);
    TEST_ASSERT_EQUAL(initState.substate, BAL_ENTRY);
}
